package cn.lyq.card.ui.rank

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.lyq.card.R
import cn.lyq.card.base.BaseFg
import cn.lyq.card.data.model.RankInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.manager.UserManager
import cn.lyq.card.utils.loadUrl
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.fg_refresh_rv.*


class RankSubFg : BaseFg() {

    private val mModel by lazy {
        getVMActivity(RankVM::class.java)
    }
    private val mIndex by lazy {
        arguments?.getInt(KEY_INDEX, 0) ?: 0
    }
    private val mAdapter by lazy {
        RankAdapter()
    }
    private val header by lazy {
        LayoutInflater.from(requireContext()).inflate(R.layout.item_rank_header, null)
    }

    companion object {
        private const val KEY_INDEX = "INDEX"

        fun newInstance(index: Int): RankSubFg {
            val args = Bundle()
            args.putInt(KEY_INDEX, index)
            val fragment = RankSubFg()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fg_refresh_rv
    }

    override fun initView(view: View) {
        super.initView(view)
        rv.layoutManager = LinearLayoutManager(requireContext())
        rv.adapter = mAdapter
        mAdapter.setHeaderView(header)
        mAdapter.setEmptyView(R.layout.layout_empty)
    }

    override fun initListener(view: View) {
        super.initListener(view)
        refreshLayout.setOnRefreshListener {
            getData()
        }
    }

    override fun initObserver() {
        super.initObserver()
        UserManager.instance.observe(this, Observer {
            it?.let {
                header.run {
                    findViewById<ImageView>(R.id.imgAvatar).loadUrl(it.userIcon)
                    findViewById<TextView>(R.id.tvUserName).text = it.agentName ?: ""
                    findViewById<TextView>(R.id.tvUserPhone).text = it.phone ?: ""
                }
            }
        })
        mModel.mTypeInfo.observe(this, Observer {
            it?.let {
                header.findViewById<TextView>(R.id.tvUserParamHint).text = it.title ?: ""
                header.findViewById<TextView>(R.id.tvParamName).text = it.title ?: ""
                getData()
            }
        })
    }

    private fun getData() {
        mModel.getRankList(mIndex + 1).observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> {
                    if (!refreshLayout.isRefreshing) {
                        refreshLayout.isRefreshing = true
                    }
                }
                Status.SUCCESS -> {
                    setHeader(it.requireData().user)
                    mAdapter.setList(it.requireData().list)
                    refreshLayout.isRefreshing = false
                }
                Status.ERROR -> {
                    mAdapter.setList(mutableListOf())
                    refreshLayout.isRefreshing = false
                    ToastUtils.showShort(it.message.getMsg())
                }
            }
        })
    }

    private fun setHeader(user: RankInfo?) {
        user?.let {
            header.findViewById<TextView>(R.id.tvUserRank).text = user.rank.toString()
            header.findViewById<TextView>(R.id.tvUserParam).text = user.number ?: "**"
        }
    }

}